package com.ruoyi.gis.controller;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.gis.domain.*;
import com.ruoyi.gis.service.IGisLayerGroupService;
import com.ruoyi.gis.service.IGisLayerProductGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.gis.service.IGisLayerProductService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * layerProductController
 *
 * @author rww
 * @date 2023-11-16
 */
@RestController
@RequestMapping("/gis/layerProduct")
public class GisLayerProductController extends BaseController
{
    @Autowired
    private IGisLayerProductService gisLayerProductService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private IGisLayerProductGroupService gisLayerProductGroupService;
    @Autowired
    private IGisLayerGroupService gisLayerGroupService;

    /**
     * 查询layerProduct列表
     */
//    @PreAuthorize("@ss.hasPermi('gis:layerProduct:list')")
    @GetMapping("/list")
    public TableDataInfo list(HttpServletRequest request, GisLayerProduct gisLayerProduct)
    {
        startPage();
        LoginUser loginUser = tokenService.getLoginUser(request);
        gisLayerProduct.setUserId(loginUser.getUserId());
        List<GisLayerProduct> list = gisLayerProductService.
                selectGisLayerProductList(gisLayerProduct);
        return getDataTable(list);
    }

    @GetMapping("/list/all")
    public AjaxResult listAll(HttpServletRequest request,
                              GisLayerProduct gisLayerProduct)
    {
        LoginUser loginUser = tokenService.getLoginUser(request);
        gisLayerProduct.setUserId(loginUser.getUserId());
        List<GisLayerProduct> list = gisLayerProductService.
                selectGisLayerProductList(gisLayerProduct);
        return AjaxResult.success(list);
    }

    @GetMapping("/list/byGroup/all")
    public AjaxResult listAllGroup(HttpServletRequest request, GisLayerProduct gisLayerProduct)
    {
        LoginUser loginUser = tokenService.getLoginUser(request);
        GisLayerProductGroup groupQuery = new GisLayerProductGroup();
        groupQuery.setUserId(loginUser.getUserId());
        List<GisLayerProductGroup> result = gisLayerProductGroupService
                .selectGisLayerProductGroupList(groupQuery);
        if(result!=null&&result.size()>0){

            result.stream().forEach(item->{
                gisLayerProduct.setUserId(loginUser.getUserId());
                gisLayerProduct.setGroupId(item.getId());
                List<GisLayerProduct> list = gisLayerProductService.selectGisLayerProductList(gisLayerProduct);
                item.setLayerProducts(list);
            });
        }
        return AjaxResult.success(result);
    }

    @GetMapping("/list/byGroup/gisLayerAllList")
    public AjaxResult gisLayerAllList(HttpServletRequest request, GisLayerProduct gisLayerProduct)
    {
        LoginUser loginUser = tokenService.getLoginUser(request);
        GisLayerProductGroup groupQuery = new GisLayerProductGroup();
        groupQuery.setUserId(loginUser.getUserId());
        groupQuery.setGroupPid(Long.parseLong("0"));
        //迭代查询
        List<GisLayerProductGroup> result = gisLayerProductGroupService.selectGisLayerProductGroupByPidList(groupQuery);
        return AjaxResult.success(result);
    }
    /**
     * 查询图层列表列表
     */
    @GetMapping("/list/byLayerGroup/gisLayerList")
    public TableDataInfo list(GisLayerGroup gisLayerGroup)
    {
        List<GisLayerGroup> list = gisLayerGroupService.selectGisLayerGroupList(gisLayerGroup);
        return getDataTable(list);
    }

    /**
     * 导出layerProduct列表
     */
//    @PreAuthorize("@ss.hasPermi('gis:layerProduct:export')")
    @Log(title = "layerProduct", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GisLayerProduct gisLayerProduct)
    {

        List<GisLayerProduct> list = gisLayerProductService.selectGisLayerProductList(gisLayerProduct);
        ExcelUtil<GisLayerProduct> util = new ExcelUtil<GisLayerProduct>(GisLayerProduct.class);
        util.exportExcel(response, list, "layerProduct数据");
    }

    /**
     * 获取layerProduct详细信息
     */
//    @PreAuthorize("@ss.hasPermi('gis:layerProduct:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(gisLayerProductService.selectGisLayerProductById(id));
    }

    /**
     * 新增layerProduct
     */
//    @PreAuthorize("@ss.hasPermi('gis:layerProduct:add')")
    @Log(title = "layerProduct", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(HttpServletRequest request,
                          @RequestBody GisLayerProductVo gisLayerProductVo)
    {
        LoginUser loginUser = tokenService.getLoginUser(request);
        gisLayerProductService.addProduct(loginUser.getUserId(),gisLayerProductVo);
        return AjaxResult.success();
    }
    @PostMapping("addGroupProduct")
    public AjaxResult addGroupProduct(HttpServletRequest request,
                          @RequestBody GisLayerGroupProductVo gisLayerGroupProductVo)
    {
        LoginUser loginUser = tokenService.getLoginUser(request);
        gisLayerProductService.addGroupProduct(loginUser.getUserId(),gisLayerGroupProductVo);
        return AjaxResult.success();
    }


    /**
     * 修改layerProduct
     */
//    @PreAuthorize("@ss.hasPermi('gis:layerProduct:edit')")
    @Log(title = "layerProduct", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GisLayerProduct gisLayerProduct)
    {
        return toAjax(gisLayerProductService.updateGisLayerProduct(gisLayerProduct));
    }

    @PostMapping("/sort")
    public AjaxResult sort(@RequestBody List<GisLayerProduct> products)
    {
        return toAjax(gisLayerProductService.updateGisLayerProductSort(products));
    }

    /**
     * 删除layerProduct
     */
//    @PreAuthorize("@ss.hasPermi('gis:layerProduct:remove')")
    @Log(title = "layerProduct", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(gisLayerProductService.deleteGisLayerProductByIds(ids));
    }
}
